package ratelimit

import (
	"context"
	_ "embed"
	"github.com/redis/go-redis/v9"
	"time"
)

//go:embed redis_windows_limit.go
var luaSideWindow string

type RedisWindowLimit struct {
	cmd redis.Cmdable
	//阈值 interval内允许 rate个请求
	rate int
	//窗口大小
	interval time.Duration
}

func (r *RedisWindowLimit) Limited(ctx context.Context, key string) (bool, error) {
	return r.cmd.Eval(ctx, luaSideWindow, []string{key}, r.interval.Microseconds(), r.rate, time.Now().UnixMilli()).Bool()
}
